#include "modifymemberover.h"
#include "ui_modifymemberover.h"
#include "personmember.h"
#include <QMessageBox>
#include "inquiresql.h"
#include <QKeyEvent>

ModifyMemberOver::ModifyMemberOver(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModifyMemberOver)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(267, 290);
    setWindowTitle(tr("修改会员余额"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_lastClick = -1;

    //正则表达式语句中不能乱加空格
    QRegExp regExpDiscount("[0-9]{1,2}|100");
    QRegExpValidator *pRegExpValidatorDiscount = new QRegExpValidator(regExpDiscount, this);
    ui->lineEditDiscount->setValidator(pRegExpValidatorDiscount);

    QRegExp regExpPrice(" [0-9]{0,6}|[0-9]{0,6}[.]{1}[0-9]{0,2} ");
    QRegExpValidator *pRegExpValidatorPrice = new QRegExpValidator(regExpPrice, this);
    ui->lineEditMemberCardAmount->setValidator(pRegExpValidatorPrice);
    ui->lineEditTotalSpending->setValidator(pRegExpValidatorPrice);

    QRegExp regExpIntegrate("[0-9]*");
    QRegExpValidator *pRegExpValidatorIntegrate = new QRegExpValidator(regExpIntegrate, this);
    ui->lineEditIntegration->setValidator(pRegExpValidatorIntegrate);
    ui->lineEditConsumeTimes->setValidator(pRegExpValidatorIntegrate);

    UpdateMemberLevel();
    eliminationPushButtonBorder();

    //添加时间过滤器
    ui->comboBoxMemberLevel->installEventFilter(this);
    ui->lineEditDiscount->installEventFilter(this);
    ui->lineEditIntegration->installEventFilter(this);
    ui->lineEditMemberCardAmount->installEventFilter(this);
    ui->lineEditTotalSpending->installEventFilter(this);
    ui->lineEditConsumeTimes->installEventFilter(this);
    ui->pushButtonSave->installEventFilter(this);
}

ModifyMemberOver::~ModifyMemberOver()
{
    delete ui;
}

//PersonMember m_personMember;
void ModifyMemberOver::displayMemberOver(const QString &MembercardID)
{
    m_personMember = m_sql->InquireMemberInformation(MembercardID);
    ui->lineEditMembercardID->setText(m_personMember.get_personID());

    int i;
    for (i = 0; i < ui->comboBoxMemberLevel->count(); ++i)
    {
        if (ui->comboBoxMemberLevel->itemText(i) == m_personMember.get_memberLevel())
        {
            ui->comboBoxMemberLevel->setCurrentIndex(i);
            break;
        }
    }
    if (!(i < ui->comboBoxMemberLevel->count()))
    {
        ui->comboBoxMemberLevel->setCurrentIndex(-1);
    }

    ui->lineEditDiscount->setText(m_personMember.get_discount());
    ui->lineEditIntegration->setText(m_personMember.get_integration());
    ui->lineEditMemberCardAmount->setText(completionPrice(m_personMember.get_membercardAmount()));
    ui->lineEditTotalSpending->setText(completionPrice(m_personMember.get_totalSpending()));
    ui->lineEditConsumeTimes->setText(m_personMember.get_consumeTimes());
}

bool ModifyMemberOver::eventFilter(QObject *target, QEvent *event)
{
    if (target == ui->comboBoxMemberLevel || target == ui->lineEditDiscount
            || target == ui->lineEditIntegration || target == ui->lineEditMemberCardAmount
            || target == ui->lineEditTotalSpending || target == ui->lineEditConsumeTimes
            || target == ui->pushButtonSave)
    {
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->comboBoxMemberLevel)
                {
                    ui->comboBoxMemberLevel->setCurrentIndex(-1);
                    m_lastClick = comboBoxMemberLevel;
                }
                if (target == ui->lineEditDiscount)
                {
                    if (m_lastClick != lineEditDiscount){
                        m_lastClick = lineEditDiscount;
                        ui->lineEditDiscount->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditDiscount)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditIntegration)
                {
                    if (m_lastClick != lineEditIntegration){
                        m_lastClick = lineEditIntegration;
                        ui->lineEditIntegration->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditIntegration)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditMemberCardAmount)
                {
                    if (m_lastClick != lineEditMemberCardAmount){
                        m_lastClick = lineEditMemberCardAmount;
                        ui->lineEditMemberCardAmount->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditMemberCardAmount)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditTotalSpending)
                {
                    if (m_lastClick != lineEditTotalSpending){
                        m_lastClick = lineEditTotalSpending;
                        ui->lineEditTotalSpending->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditTotalSpending)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditConsumeTimes)
                {
                    if (m_lastClick != lineEditConsumeTimes){
                        m_lastClick = lineEditConsumeTimes;
                        ui->lineEditConsumeTimes->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditConsumeTimes)
                    {
                        goto flag;
                    }
                }
                if (target == ui->pushButtonSave)
                {
                    m_lastClick = pushButtonSaveOver;
                    goto flag;
                }
            }
        }

        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditMembercardID)
                {
                    ui->comboBoxMemberLevel->setFocus();
                    ui->comboBoxMemberLevel->showPopup();
                }
                if (target == ui->comboBoxMemberLevel)
                {
                    ui->lineEditDiscount->setFocus();
                    ui->lineEditDiscount->selectAll();
                    return true;
                }
                if (target == ui->lineEditDiscount)
                {
                    ui->lineEditIntegration->setFocus();
                    ui->lineEditIntegration->selectAll();
                    return true;
                }
                if (target == ui->lineEditIntegration)
                {
                    ui->lineEditMemberCardAmount->setFocus();
                    ui->lineEditMemberCardAmount->selectAll();
                    return true;
                }
                if (target == ui->lineEditMemberCardAmount)
                {
                    ui->lineEditTotalSpending->setFocus();
                    ui->lineEditTotalSpending->selectAll();
                    return true;
                }
                if (target == ui->lineEditTotalSpending)
                {
                    ui->lineEditConsumeTimes->setFocus();
                    ui->lineEditConsumeTimes->selectAll();
                    return true;
                }
                if (target == ui->lineEditConsumeTimes)
                {
                    ui->pushButtonSave->setFocus();
                    return true;
                }
                if (target == ui->pushButtonSave)
                {
                    on_pushButtonSave_clicked();
                    return true;
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}

void ModifyMemberOver::UpdateMemberLevel()
{
    ui->comboBoxMemberLevel->clear();
    ui->comboBoxMemberLevel->addItems(m_sql->inquireAllMemberLevelValue());
    ui->comboBoxMemberLevel->setCurrentIndex(-1);
}

void ModifyMemberOver::on_comboBoxMemberLevel_currentIndexChanged(int index)
{
    if (index == -1)
    {
        initMemberLevel();
        return;
    }

    const memberList MemberList = m_sql->InquireMemberLevel();
    ui->lineEditIntegration->setText(MemberList.at(index).get_integration());
    ui->lineEditDiscount->setText(MemberList.at(index).get_discount());
    ui->lineEditMemberCardAmount->setText(completionPrice(MemberList.at(index).get_membercardAmount()));
}

void ModifyMemberOver::eliminationPushButtonBorder()
{
    //消除边框
    ui->pushButtonSave->setFlat(true);
    ui->pushButtonCancel->setFlat(true);
}

void ModifyMemberOver::initMemberLevel()
{
    ui->lineEditIntegration->setText("0");
    ui->lineEditDiscount->setText("100");
    ui->lineEditMemberCardAmount->setText("0.00");
}

QString ModifyMemberOver::completionPrice(const QString &price)
{
    QString number = price;
    if (number.isEmpty())
    {
        number.append("0.00");
        return number;
    }
    if (number.contains(".", Qt::CaseInsensitive))
    {
        if (number.left(1) == ".")
        {
            number.replace(0, 1, "0.");
            return number;
        }
        if (number.right(1) == ".")
        {
            number.append("00");
            return number;
        }
        if (number.right(2).contains(".", Qt::CaseInsensitive))
        {
            number.append("0");
            return number;
        }
    }
    else
    {
        number.append(".00");
        return number;
    }

    return NULL;
}

void ModifyMemberOver::on_lineEditMemberCardAmount_editingFinished()
{
    ui->lineEditMemberCardAmount->setText(
                completionPrice(ui->lineEditMemberCardAmount->text()));
}

void ModifyMemberOver::on_lineEditTotalSpending_editingFinished()
{
    ui->lineEditTotalSpending->setText(
                completionPrice(ui->lineEditTotalSpending->text()));
}

void ModifyMemberOver::on_pushButtonSave_clicked()
{
    m_personMember.set_memberLevel(ui->comboBoxMemberLevel->currentText());
    m_personMember.set_discount(ui->lineEditDiscount->text());
    m_personMember.set_integration(ui->lineEditIntegration->text());
    m_personMember.set_membercardAmount(ui->lineEditMemberCardAmount->text());
    m_personMember.set_totalSpending(ui->lineEditTotalSpending->text());
    m_personMember.set_consumeTimes(ui->lineEditConsumeTimes->text());
    if (!m_sql->UpdatMemberOver(m_personMember))
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n修改会员信息失败!"));
    }
    else
    {
        emit updateMemberProfile();
        this->hide();
    }
}

void ModifyMemberOver::on_pushButtonCancel_clicked()
{
    this->hide();
}

void ModifyMemberOver::on_lineEditDiscount_editingFinished()
{
    ui->lineEditDiscount->setText(QString::number(ui->lineEditDiscount->text().toInt()));
}

void ModifyMemberOver::on_lineEditConsumeTimes_editingFinished()
{
    ui->lineEditConsumeTimes->setText(QString::number(ui->lineEditConsumeTimes->text().toInt()));
}
